﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IP.Core.IPData;
using IP.Core.IPUserService;
using BKIDS;
using BKIDS.CDBNames;
using BKIUS;
using IP.Core.IPCommon;
using System.Data;

public partial class DanhMuc_F600_Du_an : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data_2_combo_trang_thai_da_s();
            load_data_2_combo_trang_thai_da();
            load_data_2_combo_loai_du_an();
            load_data_2_combo_khach_hang();
        }
    }

    #region Members
    US_DM_DU_AN m_us_du_an = new US_DM_DU_AN();
    DS_DM_DU_AN m_ds_du_an = new DS_DM_DU_AN();
    #endregion

    #region Private Methods
    private void load_data_2_grv()
    {
        US_DM_DU_AN v_us_du_an = new US_DM_DU_AN();
        DS_DM_DU_AN v_ds_du_an = new DS_DM_DU_AN();

        v_us_du_an.FillDataset(v_ds_du_an);

        m_grv_ds_du_an.DataSource = v_ds_du_an.DM_DU_AN;
        m_grv_ds_du_an.DataBind();
    }
    private void get_data_2_ds_export_excel()
    {
        // Thu thập dữ liệu từ form tìm kiếm
        string v_str_ma_du_an = m_txt_ma_du_an_s.Text.Trim();
        decimal v_dc_id_trang_thai_du_an = CIPConvert.ToDecimal(m_cbo_trang_thai_du_an_s.SelectedValue);
        string v_str_tu_khoa = m_txt_tu_khoa_tim_kiem.Text.Trim();
        // Search va load dât vào ds, ko bind len luoi
        m_us_du_an.loc_du_lieu_du_an(m_ds_du_an, v_str_ma_du_an, v_dc_id_trang_thai_du_an, v_str_tu_khoa);
    }
    private void search_du_an_and_load_2_grv()
    { 
        // Thu thập dữ liệu từ form tìm kiếm
        string v_str_ma_du_an = m_txt_ma_du_an_s.Text.Trim();
        decimal v_dc_id_trang_thai_du_an = CIPConvert.ToDecimal(m_cbo_trang_thai_du_an_s.SelectedValue);
        string v_str_tu_khoa = m_txt_tu_khoa_tim_kiem.Text.Trim();
        // Search
        m_us_du_an.loc_du_lieu_du_an(m_ds_du_an, v_str_ma_du_an, v_dc_id_trang_thai_du_an, v_str_tu_khoa);
        m_grv_ds_du_an.Visible = true;
        m_grv_ds_du_an.DataSource = m_ds_du_an.DM_DU_AN;
        m_grv_ds_du_an.DataBind();
        m_lbl_cap_nhat.Text = "Kết quả lọc dữ liệu: Có " + m_ds_du_an.DM_DU_AN.Rows.Count + " bản ghi phù hợp.";
        if (m_ds_du_an.DM_DU_AN.Rows.Count == 0)
        {
            m_lbl_cap_nhat.Text = "Kết quả lọc dữ liệu";
            m_lbl_thong_bao.Text = "* Không có bản ghi nào phù hợp.";
        }
        
    }
    private void load_data_2_combo_trang_thai_da_s()
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_tu_dien = new DS_CM_DM_TU_DIEN();

        v_us_tu_dien.FillDataset(v_ds_tu_dien,"WHERE ID_LOAI_TU_DIEN=2");
        m_cbo_trang_thai_du_an_s.Items.Add(new ListItem("Tất cả", CIPConvert.ToStr(0)));
        for (int v_i = 0; v_i < v_ds_tu_dien.CM_DM_TU_DIEN.Rows.Count; v_i++)
        {
            m_cbo_trang_thai_du_an_s.Items.Add(new ListItem(CIPConvert.ToStr(v_ds_tu_dien.CM_DM_TU_DIEN.Rows[v_i][CM_DM_TU_DIEN.TEN]),CIPConvert.ToStr(v_ds_tu_dien.CM_DM_TU_DIEN.Rows[v_i][CM_DM_TU_DIEN.ID])));
        }
    }
    /// <summary>
    /// Lấy dữ liệu từ CSDL đổ vài US
    /// </summary>
    private void load_data_2_us_update(int ip_i_stt_row)
    {
        // Lấy Id dựa vào số TT dòng
        decimal v_dc_id_du_an =CIPConvert.ToDecimal(m_grv_ds_du_an.DataKeys[ip_i_stt_row].Value);
        // Lưu ID có 2 cách
        //C1 Dùng HiddenField
        //m_hdf_id_dm_du_an.Value = CIPConvert.ToStr(v_dc_id_du_an);
        //C2 Dùng Tooltip của 1 control có sẵn
        m_txt_ma_du_an.ToolTip = CIPConvert.ToStr(v_dc_id_du_an);
        // Lấy dữ liệu từ CSDL dựa vào ID vừa có
        m_us_du_an = new US_DM_DU_AN(v_dc_id_du_an);
    }
    /// <summary>
    /// Lấy dữ liệu từ US và đổ lên form
    /// </summary>
    private void us_object_2_form(US_DM_DU_AN ip_us_v_dm_du_an)
    {
        m_txt_ma_du_an.Text = ip_us_v_dm_du_an.strMA_DU_AN;
        m_txt_ten_du_an.Text = ip_us_v_dm_du_an.strTEN_DU_AN;
        m_cbo_loai_du_an.SelectedValue = CIPConvert.ToStr(ip_us_v_dm_du_an.dcID_LOAI_DU_AN);
        if (!m_us_du_an.IsNGAY_KY_HOP_DONGNull()) m_txt_ngay_ky_hd.SelectedDate = CIPConvert.ToDatetime(CIPConvert.ToStr(m_us_du_an.datNGAY_KY_HOP_DONG, "dd/MM/yyyy"), "dd/MM/yyyy");
        m_cbo_trang_thai_du_an.SelectedValue = CIPConvert.ToStr(ip_us_v_dm_du_an.dcID_TRANG_THAI_DU_AN);
        m_txt_gia_tri_hop_dong.Text = CIPConvert.ToStr(ip_us_v_dm_du_an.dcGIA_TRI_HOP_DONG,"#,###");
        m_txt_gia_tri_thuc_nhan.Text = CIPConvert.ToStr(ip_us_v_dm_du_an.dcGIA_TRI_THUC_NHAN,"#,###");
        m_cbo_khach_hang.SelectedValue = CIPConvert.ToStr(ip_us_v_dm_du_an.dcID_KHACH_HANG);
        if (ip_us_v_dm_du_an.strCO_THUE_SUAT_YN.Equals("Y")) m_rdl_tax.Items[0].Selected = true;
        else m_rdl_tax.Items[1].Selected = true;
        m_txt_ghi_chu.Text = ip_us_v_dm_du_an.strGHI_CHU;
    }
    private void load_data_2_combo_trang_thai_da()
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_tu_dien = new DS_CM_DM_TU_DIEN();

        v_us_tu_dien.FillDataset(v_ds_tu_dien, "WHERE ID_LOAI_TU_DIEN=2");

        m_cbo_trang_thai_du_an.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_trang_thai_du_an.DataValueField = CM_DM_TU_DIEN.ID;

        m_cbo_trang_thai_du_an.DataSource = v_ds_tu_dien.CM_DM_TU_DIEN;
        m_cbo_trang_thai_du_an.DataBind();
    }
    private void load_data_2_combo_loai_du_an()
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_tu_dien = new DS_CM_DM_TU_DIEN();

        v_us_tu_dien.FillDataset(v_ds_tu_dien, "WHERE ID_LOAI_TU_DIEN=5");

        m_cbo_loai_du_an.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_loai_du_an.DataValueField = CM_DM_TU_DIEN.ID;

        m_cbo_loai_du_an.DataSource = v_ds_tu_dien.CM_DM_TU_DIEN;
        m_cbo_loai_du_an.DataBind();
    }
    private void load_data_2_combo_khach_hang()
    {
        US_DM_KHACH_HANG v_us_khach_hang = new US_DM_KHACH_HANG();
        DS_DM_KHACH_HANG v_ds_khach_hang = new DS_DM_KHACH_HANG();

        v_us_khach_hang.FillDataset(v_ds_khach_hang);
        // Add thêm 1 item cho cbo không có trong CSDL
        //m_cbo_khach_hang.Items.Add(new ListItem("Chọn khách hàng", CIPConvert.ToStr(0)));
        for (int v_i = 0; v_i < v_ds_khach_hang.DM_KHACH_HANG.Rows.Count; v_i++)
        {
            m_cbo_khach_hang.Items.Add(new ListItem(CIPConvert.ToStr(v_ds_khach_hang.DM_KHACH_HANG.Rows[v_i][DM_KHACH_HANG.HO_TEN]), CIPConvert.ToStr(v_ds_khach_hang.DM_KHACH_HANG.Rows[v_i][DM_KHACH_HANG.ID])));
            
        }
        // Cách load dữ liệu lên cbo khác
       // m_cbo_khach_hang.DataTextField = DM_KHACH_HANG.HO_TEN;
        //m_cbo_khach_hang.DataValueField = DM_KHACH_HANG.ID;
        //m_cbo_khach_hang.DataSource = v_ds_khach_hang.DM_KHACH_HANG;
        //m_cbo_khach_hang.DataBind();
    }
     private bool check_exist_du_an(string ip_str_ma_du_an, out string op_str_du_an_trung)
    {
        m_us_du_an.check_trung_ma_du_an(m_ds_du_an, ip_str_ma_du_an);
        op_str_du_an_trung = "";
        if (m_ds_du_an.DM_DU_AN.Rows.Count > 0) 
        {
            op_str_du_an_trung = "Mã dự án: " + ip_str_ma_du_an + " đã tồn tại với tên là: " + CIPConvert.ToStr(m_ds_du_an.DM_DU_AN.Rows[0][DM_DU_AN.TEN_DU_AN]);
            return false;
        }
        return true;
    }
    // insert 
    private void form_2_us_object()
    {
        m_us_du_an.strMA_DU_AN = m_txt_ma_du_an.Text;
        m_us_du_an.strTEN_DU_AN = m_txt_ten_du_an.Text;
        m_us_du_an.dcID_LOAI_DU_AN = CIPConvert.ToDecimal(m_cbo_loai_du_an.SelectedValue);
        if (m_txt_ngay_ky_hd.Text != "")
        {
            m_us_du_an.datNGAY_KY_HOP_DONG = m_txt_ngay_ky_hd.SelectedDate;
        }
        m_us_du_an.dcID_TRANG_THAI_DU_AN = CIPConvert.ToDecimal(m_cbo_trang_thai_du_an.SelectedValue);
        if (m_txt_gia_tri_hop_dong.Text != "")
        {
            m_us_du_an.dcGIA_TRI_HOP_DONG = CIPConvert.ToDecimal(m_txt_gia_tri_hop_dong.Text);
        }
        if (m_txt_gia_tri_thuc_nhan.Text != "")
        {
            m_us_du_an.dcGIA_TRI_THUC_NHAN = CIPConvert.ToDecimal(m_txt_gia_tri_thuc_nhan.Text);
        }
        m_us_du_an.dcID_KHACH_HANG = CIPConvert.ToDecimal(m_cbo_khach_hang.SelectedValue);
        m_us_du_an.strCO_THUE_SUAT_YN = m_rdl_tax.SelectedValue;
        m_us_du_an.strGHI_CHU = m_txt_ghi_chu.Text;
    }
    #endregion

    #region Export Excel
    private void loadDSExprort(ref string strTable)
    {
        int v_i_so_thu_tu = 0;
        // Mỗi cột dữ liệu ứng với từng dòng là label
        foreach (DataRow grv in this.m_ds_du_an.DM_DU_AN.Rows)
        {
            // Bước này thì ko khác gì bind lên Gridview
            // Các cột ngoài Gridview viết thế nào, và o đây cũng thế
            strTable += "\n<tr>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + ++v_i_so_thu_tu + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[DM_DU_AN.MA_DU_AN]).Trim() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[DM_DU_AN.TEN_DU_AN]).Trim() + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_loai_du_an_by_id(CIPConvert.ToDecimal(grv[DM_DU_AN.ID_LOAI_DU_AN])) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[DM_DU_AN.GIA_TRI_HOP_DONG]) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_khach_hang_by_id(CIPConvert.ToDecimal(grv[DM_DU_AN.ID_KHACH_HANG])) + "</td>";
            strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_trang_thai_da_by_id(grv[DM_DU_AN.ID_TRANG_THAI_DU_AN]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_magv_by_id(CIPConvert.ToDecimal(grv[V_GD_THANH_TOAN.ID_GIANG_VIEN])) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + CIPConvert.ToStr(grv[V_GD_THANH_TOAN.TEN_GIANG_VIEN]).Trim() + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + "'" + mapping_string(grv[V_GD_THANH_TOAN.SO_TAI_KHOAN]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_string(grv[V_GD_THANH_TOAN.TEN_NGAN_HANG]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + "'" + mapping_string(grv[V_GD_THANH_TOAN.MA_SO_THUE]) + "</td>";
            
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_item_field_nghiem_thu_lop_mon(CIPConvert.ToStr(grv[V_GD_THANH_TOAN.LOAI_HOP_DONG]), grv[V_GD_THANH_TOAN.REFERENCE_CODE], grv[V_GD_THANH_TOAN.GIA_TRI_NGHIEM_THU_THUC_TE]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_item_field_ten_cac_mon(CIPConvert.ToStr(grv[V_GD_THANH_TOAN.LOAI_HOP_DONG]), grv[V_GD_THANH_TOAN.GHI_CHU_CAC_MON_PHU_TRACH]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_so_tien(grv[V_GD_THANH_TOAN.DA_THANH_TOAN]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_so_tien(grv[V_GD_THANH_TOAN.TONG_TIEN_THANH_TOAN]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_so_tien(grv[V_GD_THANH_TOAN.SO_TIEN_THUE]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_so_tien(grv[V_GD_THANH_TOAN.TONG_TIEN_THUC_NHAN]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_so_tien_con_phai_tt(grv[V_GD_THANH_TOAN.CON_PHAI_THANH_TOAN], grv[V_GD_THANH_TOAN.LOAI_HOP_DONG], grv[V_GD_THANH_TOAN.REFERENCE_CODE]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_trang_thai_thanh_toan(CIPConvert.ToDecimal(grv[V_GD_THANH_TOAN.ID_TRANG_THAI_THANH_TOAN])) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_string(grv[V_GD_THANH_TOAN.PO_LAP_THANH_TOAN]) + "</td>";
            //strTable += "\n<td style='width:12%;' class='cssTitleReport' nowrap='nowrap'>" + mapping_string(grv[V_GD_THANH_TOAN.DESCRIPTION]) + "</td>";
            strTable += "\n</tr>";
        }
    }

    private void loadTieuDe(ref string strTable)
    {
        // Hàm này load dữ liệu vào DS toàn cục
        get_data_2_ds_export_excel();

        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'><img src='~/Images/cal.gif'</td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n</tr>";

        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssZent' style='width:100%;' nowrap='nowrap'>ZentBKa</td>";
        strTable += "\n</tr>";

        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTitleReport' style='width: 100%;  height: 40px; font-size: 30px; color:White; background-color:#0000ff;' nowrap='wrap'>BKI - PRO600 - BÁO CÁO DANH CÁC DỰ ÁN" + "</td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width: 100%;  height: 40px; font-size: 30px; color:White; background-color:#0000ff;' nowrap='wrap'>Ngày xuất báo cáo: " +DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day + "</td>";
        strTable += "\n</tr>";

        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width: 100%;  height: 40px; font-size: 30px; color:White; background-color:#0000ff;' nowrap='wrap'>Ngày xuất báo cáo: " + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + "</td>";
        strTable += "\n</tr>";

        // môi cai nayla 1 dong, cai nay la header cua bao cao, hien thi: ten bao cao, các tham so bao cao (ngay, dau vao bao cao,...
        //
        strTable += "\n<tr>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>  </td>";
        //strTable += "\n<td><align='center' class='cssTableView' style='width:100%;' nowrap='nowrap'>Ngày hoàn tất thanh toán : " + m_lbl_ngay_hoan_tat_tt.Text + "</td>";
        strTable += "\n</tr>";
        strTable += "\n</table>";
        //table noi dung
        strTable += "<table cellpadding='2' cellspacing='0' class='cssTableReport'>";
        // còn đây là tên các cột trong báo cáo
        strTable += "\n<tr>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>STT</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Mã Dự án</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Tên dự án</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Loại dự án</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Giá trị hợp đồng</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Khách hàng</td>";
        strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Trạng thái dự án</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Số tài khoản</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Tên ngân hàng</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Mã số thuế</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Tổng giá trị HĐ (VNĐ)</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Tên các môn phụ trách</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Đã thanh toán (VNĐ)</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Tổng tiền thanh toán đợt này (VNĐ)</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Số tiền thuế (VNĐ)</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Tổng tiền thực nhận đợt này (VNĐ)</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Số tiền còn phải thanh toán (VNĐ)</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Trạng thái thanh toán</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>PO lập thanh toán</td>";
        //strTable += "\n<td style='width:12%;' class='cssTableView' nowrap='nowrap'>Ghi chú</td>";
        strTable += "\n</tr>";
        // Sau đó, đổ dữ liệu lên cái bảng đó
        loadDSExprort(ref strTable);
        strTable += "\n</table>";
    }

    private string loadExport()
    {
        try
        {
            string strHTML = "<html xmlns:o='urn:schemas-microsoft-com:office:office'"
            + "\n xmlns:x='urn:schemas-microsoft-com:office:excel'"
            + "\n xmlns='http://www.w3.org/TR/REC-html40'>"
            + "\n <head>"
            + "\n <meta http-equiv=Content-Type content='text/html; charset=utf-8'>"
            + "\n <meta name=ProgId content=Excel.Sheet>"
            + "\n <meta name=Generator content='Microsoft Excel 11'>"
            + "\n <link rel=File-List href='Book1_files/filelist.xml'>"
            + "\n <style id='Book1_28091_Styles'><!--table"
            + "\n 	{mso-displayed-decimal-separator:'\\.';"
            + "\n 	mso-displayed-thousand-separator:'\\,';}"
            + ".cssTitleReport"
            + "{font-family: tahoma; font-size:18px;font-weight:normal;border: 1px #000000 solid;text-align:left;}"
            + ".cssZent"
            + "{font-family: tahoma; font-size:20px;font-weight:normal;text-align:left;}"
            + ".cssTableView"
            + "{color:#FFFFFF;background-color:#0000ff;font-family: tahoma,Arial,Times New Roman; font-size: 12px;font-weight:bold;border: 1px #000000 solid;}"
            + "\n 	--></style>"
            + "\n 	</head>"
            + "\n 	<body><div id='Book1_28091' align=center x:publishsource='Excel'>";
            string strTable = "";
            loadTieuDe(ref strTable);
            strHTML += strTable;
            strHTML += "\n </div></body> ";
            strHTML += "\n </html> ";

            return strHTML;
        }
        catch
        {
            return "";
        }
    }
    #endregion

    #region Public Interfaces
    public string mapping_loai_du_an_by_id(object ip_obj_id_loai_du_an)
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN(CIPConvert.ToDecimal(ip_obj_id_loai_du_an));
        return v_us_tu_dien.strTEN;
    }
    public string mapping_khach_hang_by_id(object ip_obj_id_khach_hang)
    {
        US_DM_KHACH_HANG v_us_khach_hang = new US_DM_KHACH_HANG(CIPConvert.ToDecimal(ip_obj_id_khach_hang));
        return v_us_khach_hang.strHO_TEN;
    }
    public string mapping_trang_thai_da_by_id(object ip_obj_id_trang_thai_da)
    {
        US_CM_DM_TU_DIEN v_us_tu_dien = new US_CM_DM_TU_DIEN(CIPConvert.ToDecimal(ip_obj_id_trang_thai_da));
        return v_us_tu_dien.strTEN;
    }
    public void hien_thi_form_cap_nhat()
    {
        m_cmd_them_moi_s.Visible = false;
        lbl_ten_da.Visible = true;
        m_txt_ten_du_an.Visible = true;
        lbl_loai_da.Visible = true;
        m_cbo_loai_du_an.Visible = true;
        lbl_ngay_ky_hd.Visible = true;
        m_txt_ngay_ky_hd.Visible = true;
        lbl_trang_thai_da.Visible = true;
        m_cbo_trang_thai_du_an.Visible = true;
        lbl_gia_tri_hd.Visible = true;
        m_txt_gia_tri_hop_dong.Visible = true;
        lbl_gia_tri_thuc_nhan.Visible = true;
        m_txt_gia_tri_thuc_nhan.Visible = true;
        lbl_khach_hang.Visible = true;
        m_cbo_khach_hang.Visible = true;
        lbl_thue.Visible = true;
        m_rdl_tax.Visible = true;
        lbl_ghi_chu.Visible = true;
        m_txt_ghi_chu.Visible = true;
        m_cmd_them.Visible = true;
        m_cmd_xoa_trang.Visible = true;
        m_txt_ma_du_an.Visible = true;
        m_lbl_ma_du_an.Visible = true;
        m_cmd_huy.Visible = true;
    }
    public void an_thi_form_cap_nhat()
    {
        m_cmd_them_moi_s.Visible = true;
        lbl_ten_da.Visible = false;
        m_txt_ten_du_an.Visible = false;
        lbl_loai_da.Visible = false;
        m_cbo_loai_du_an.Visible = false;
        lbl_ngay_ky_hd.Visible = false;
        m_txt_ngay_ky_hd.Visible = false;
        lbl_trang_thai_da.Visible = false;
        m_cbo_trang_thai_du_an.Visible = false;
        lbl_gia_tri_hd.Visible = false;
        m_txt_gia_tri_hop_dong.Visible = false;
        lbl_gia_tri_thuc_nhan.Visible = false;
        m_txt_gia_tri_thuc_nhan.Visible = false;
        lbl_khach_hang.Visible = false;
        m_cbo_khach_hang.Visible = false;
        lbl_thue.Visible = false;
        m_rdl_tax.Visible = false;
        lbl_ghi_chu.Visible = false;
        m_txt_ghi_chu.Visible = false;
        m_cmd_them.Visible = false;
        m_cmd_xoa_trang.Visible = false;
        m_txt_ma_du_an.Visible = false;
        m_lbl_ma_du_an.Visible = false;
        m_cmd_huy.Visible = false;
        m_cmd_cap_nhat.Visible = false;
        m_cmd_sua_thong_tin_du_an.Visible = false;
    }
    public void xoa_trang()
    {
        m_txt_ma_du_an.Text = "";
        m_txt_ten_du_an.Text = "";
        m_txt_ngay_ky_hd.Text = "";
        m_txt_gia_tri_hop_dong.Text = "";
        m_txt_gia_tri_thuc_nhan.Text = "";
        m_txt_ghi_chu.Text = "";
    }
    public void Enabled_form_xem_chi_tiet()
    {
        m_txt_ma_du_an.Enabled = false;
        m_txt_ten_du_an.Enabled = false;
        m_cbo_loai_du_an.Enabled = false;
        m_txt_ngay_ky_hd.Enabled = false;
        m_cbo_trang_thai_du_an.Enabled = false;
        m_txt_gia_tri_hop_dong.Enabled = false;
        m_txt_gia_tri_thuc_nhan.Enabled = false;
        m_cbo_khach_hang.Enabled = false;
        m_rdl_tax.Enabled = false;
        m_txt_ghi_chu.Enabled = false;
        m_cmd_sua_thong_tin_du_an.Visible = true;
    }
    public void Enabled_form_cap_nhat()
    {
        m_txt_ma_du_an.Enabled = false;
        m_txt_ten_du_an.Enabled = true;
        m_cbo_loai_du_an.Enabled = true;
        m_txt_ngay_ky_hd.Enabled = true;
        m_cbo_trang_thai_du_an.Enabled = true;
        m_txt_gia_tri_hop_dong.Enabled = true;
        m_txt_gia_tri_thuc_nhan.Enabled = true;
        m_cbo_khach_hang.Enabled = true;
        m_rdl_tax.Enabled = true;
        m_txt_ghi_chu.Enabled = true;
        m_cmd_cap_nhat.Visible = true;
    }
    #endregion
    protected void m_cmd_xem_toan_bo_Click(object sender, EventArgs e)
    {
            m_lbl_cap_nhat.Text = "Danh sách dự án";
            load_data_2_grv();
            an_thi_form_cap_nhat();
            m_cmd_them_moi_s.Visible = true;
            m_grv_ds_du_an.Visible = true;
            m_lbl_thong_bao.Visible = false;
    }
    protected void m_cmdbtn_them_moi_s_Click(object sender, EventArgs e)
    {
        hien_thi_form_cap_nhat();
        xoa_trang();
        m_lbl_cap_nhat.Text = "Thêm mới dự án";
        m_grv_ds_du_an.Visible = false;
    }
    protected void m_cmd_xoa_trang_Click(object sender, EventArgs e)
    {
        xoa_trang();
        
    }
    protected void m_cmd_loc_du_lieu_Click(object sender, EventArgs e)
    {
        search_du_an_and_load_2_grv();
        //load_data_2_grv();
        an_thi_form_cap_nhat();
        m_cmd_them_moi_s.Visible = true;
        m_grv_ds_du_an.Visible = true;
        m_lbl_thong_bao.Visible = false;
    }
    protected void m_cmd_them_Click(object sender, EventArgs e)
    {
        string v_str_result = "";
        if (!check_exist_du_an(m_txt_ma_du_an.Text, out v_str_result))
        {
            string v_str_script = string.Format("<script language='javascript'> alert('{0}'); </script>", v_str_result);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Oncheck", v_str_script);
            m_txt_ma_du_an.Focus();
            m_txt_ma_du_an.Text = "";
            return;
        }
        form_2_us_object();
        m_us_du_an.Insert();
        an_thi_form_cap_nhat();
        m_lbl_thong_bao.Visible = true;
        m_lbl_thong_bao.Text = "Đã thêm mới dự án thành công !";
        m_lbl_cap_nhat.Text = "Danh sách dự án";
        xoa_trang();
        m_grv_ds_du_an.Visible = true;
        load_data_2_grv();

    }
    protected void m_cmd_huy_Click(object sender, EventArgs e)
    {
            m_lbl_cap_nhat.Text = "Kết quả lọc dữ liệu";
            an_thi_form_cap_nhat();
            m_cmd_them_moi_s.Visible = true;
            m_grv_ds_du_an.Visible = false;
    }
    protected void m_grv_ds_du_an_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            m_lbl_cap_nhat.Text = "Thông tin chi tiết dự án";
            load_data_2_us_update(e.RowIndex);
            us_object_2_form(m_us_du_an);
            m_grv_ds_du_an.Visible = false;
            hien_thi_form_cap_nhat();

            m_cmd_them.Visible = false;
            m_cmd_xoa_trang.Visible = false;
            m_cmd_huy.Visible = false;
            m_lbl_thong_bao.Visible = false;

            Enabled_form_xem_chi_tiet();
        }
        catch (Exception)
        {

        }
    }
    protected void m_cmd_sua_thong_tin_du_an_Click(object sender, EventArgs e)
    {
        Enabled_form_cap_nhat();
        m_cmd_sua_thong_tin_du_an.Visible = false;
        m_cmd_huy.Visible = true;
    }
    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        form_2_us_object();
        //m_us_du_an.dcID = CIPConvert.ToDecimal(m_hdf_id_dm_du_an.Value);
        m_us_du_an.dcID = CIPConvert.ToDecimal(m_txt_ma_du_an.ToolTip);
        m_us_du_an.Update();
        an_thi_form_cap_nhat();
        m_grv_ds_du_an.Visible = true;
        m_lbl_thong_bao.Visible = true;
        m_lbl_thong_bao.Text = " * Cập nhật thành công !";
        load_data_2_grv();
        // Sau đó phải xóa trắng m_txt_ma_du_an hoặc m_hdf_id_dm_du_an
        //m_hdf_id_dm_du_an.Value = "";
        m_txt_ma_du_an.ToolTip = "";
    }
    protected void m_grv_ds_du_an_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        decimal v_dc_id_du_an =CIPConvert.ToDecimal(m_grv_ds_du_an.DataKeys[e.RowIndex].Value);
        m_us_du_an.DeleteByID(v_dc_id_du_an);
        m_lbl_thong_bao.Visible = true;
        m_lbl_thong_bao.Text = " * Xóa bản ghi thành công !";
        load_data_2_grv();
        // Sau đó phải xóa trắng m_txt_ma_du_an hoặc m_hdf_id_dm_du_an
        //m_hdf_id_dm_du_an.Value = "";
        m_txt_ma_du_an.ToolTip = "";
    }
    protected void m_cmd_xuat_excel_Click(object sender, EventArgs e)
    {
        try
        {
            string html = loadExport();
            string strNamFile = "BKI.PRO.BaoCaoDanhSachDuAn-" + DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day + ".xls";
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(1));
            Response.Clear();
            Response.AppendHeader("content-disposition", "attachment;filename=" + strNamFile);
            Response.Charset = "UTF-8";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "text/csv";
            Response.ContentType = "application/vnd.ms-excel";
            this.EnableViewState = false;
            Response.Write("\r\n");
            Response.Write(html);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        catch (Exception v_e)
        {
            Response.Write(v_e.Message);
        }
    }
}